package com.yiqi.liebang.feature.home.view;


import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.rxbus2.RxBus;
import com.luck.picture.lib.rxbus2.Subscribe;
import com.luck.picture.lib.rxbus2.ThreadMode;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.framework.Callback;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.util.EventEntity;
import com.yiqi.liebang.entity.bo.SearchFriendBo;
import com.yiqi.liebang.feature.home.contract.FriendsContract;
import com.yiqi.liebang.feature.home.dependencies.friends.DaggerFriendsComponent;
import com.yiqi.liebang.feature.home.dependencies.friends.FriendsPresenterModule;
import com.yiqi.liebang.feature.home.view.adapter.FriendsAdapter;
import com.yiqi.liebang.feature.home.view.adapter.PeopleAdapter;
import com.yiqi.liebang.feature.mine.view.UserInfoActivity;
import com.yiqi.liebang.feature.people.view.FriendsListActivity;
import com.yiqi.liebang.feature.people.view.PeopleActivity;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.Unbinder;
import io.reactivex.disposables.Disposable;

/**
 * A simple {@link Fragment} subclass.
 */
public class FriendsFragment extends BaseFragment implements
        FriendsContract.View, BaseQuickAdapter.RequestLoadMoreListener, OnRefreshListener {

    @Inject
    FriendsContract.Presenter mPresenter;

    @BindView(R.id.rv_search)
    RecyclerView mRvFriends;
    @BindView(R.id.rv_search2)
    RecyclerView mRvPeople;
    FriendsAdapter mAdapterFriends;
    PeopleAdapter mAdapterPeople;
//    @BindView(R.id.smartRefesh)
//    SmartRefreshLayout mSmartRefesh;
    Unbinder unbinder;
    private EmptyView mEmptyView;
    private EmptyView mEmptyView2;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<SearchFriendBo.UserFriendBean> mData;

    private String mContent;

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_friends;
    }

    @Override
    protected void initInjector() {
        DaggerFriendsComponent.builder().friendsPresenterModule(new FriendsPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {

        initAdpter();
        super.initView();
        if (!RxBus.getDefault().isRegistered(this)) {
            RxBus.getDefault().register(this);
        }

    }

    /**
     * EventBus 3.0 回调
     *
     * @param obj
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventBus(EventEntity obj) {
        switch (obj.what) {
            case 1:
                mContent = obj.medias;
                pageIndex = 1;
                mPresenter.searchFriend(mContent, pageIndex, pageSize);
                break;
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (RxBus.getDefault().isRegistered(this)) {
            RxBus.getDefault().unregister(this);
        }
    }

    private void initAdpter() {

        mEmptyView = new EmptyView(mRvFriends);
        mRvFriends.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvFriends.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mAdapterFriends = new FriendsAdapter(false, getActivity());
        mAdapterFriends.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mAdapterFriends.bindToRecyclerView(mRvFriends);
        mAdapterFriends.setOnLoadMoreListener(this, mRvFriends);
        mAdapterFriends.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), UserInfoActivity.class);
                intent.putExtra("is_view_my", false);
                intent.putExtra("visitorUid", mAdapterFriends.getData().get(position).getUserUid());
                startActivity(intent);
            }
        });
        View view = getHeaderView(1, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), FriendsListActivity.class));
            }
        });
        mAdapterFriends.addFooterView(view);


        mEmptyView2 = new EmptyView(mRvPeople);
        mRvPeople.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvPeople.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mAdapterPeople = new PeopleAdapter(true);
        mAdapterPeople.bindToRecyclerView(mRvPeople);
        mAdapterPeople.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mAdapterPeople.setCommonCallback(new Callback.CommonCallback() {
            @Override
            public void onSubscribe(Disposable d) {
                showLoading(d);
            }

            @Override
            public void onSuccess(Object result) {
                dismissLoading();
            }

            @Override
            public void onError(Throwable ex) {
                dismissLoading();
            }

            @Override
            public void onComplete() {
                dismissLoading();
            }
        });
        View view2 = getHeaderView(2, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), PeopleActivity.class));
            }
        });
        mAdapterPeople.addFooterView(view2);
        mAdapterPeople.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), UserInfoActivity.class);
                intent.putExtra("is_view_my", false);
                intent.putExtra("visitorUid", mAdapterPeople.getData().get(position).getUserUid());
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

    }

    private View getHeaderView(int type, View.OnClickListener listener) {
        View view = null;
        if (type == 1) {
            view = getLayoutInflater().inflate(R.layout.item_friends_footer, (ViewGroup) mRvFriends.getParent(), false);
            TextView imageView = view.findViewById(R.id.tv_friends_footer);
            imageView.setText("更多好友>>");
        } else if (type == 2) {
            view = getLayoutInflater().inflate(R.layout.item_friends_footer, (ViewGroup) mRvPeople.getParent(), false);
            TextView imageView = view.findViewById(R.id.tv_friends_footer);
            imageView.setText("更多人脉>>");
        }
        view.setOnClickListener(listener);
        return view;
    }

    @Override
    public void showFriendsListSuccess(List<SearchFriendBo.UserFriendBean> userFriendBeans) {

        mData = userFriendBeans;
        setData(true, mData);
        mAdapterFriends.setEnableLoadMore(true);
    }

    @Override
    public void showFriendsListError(String error) {

//        showMsg(error);
        mAdapterFriends.setEmptyView(mEmptyView.getErrorView(error));
        mAdapterFriends.setNewData(null);

    }

    @Override
    public void showFriendsListMoreSuccess(List<SearchFriendBo.UserFriendBean> userFriendBeans) {

        setData(false, userFriendBeans);
    }

    @Override
    public void showFriendsListMoreError(String msg) {
        mAdapterFriends.loadMoreFail();
    }

    @Override
    public void showFriendsListEmpty() {

        mAdapterFriends.setEmptyView(mEmptyView.getEmptyView("暂无好友"));
        mAdapterFriends.setNewData(null);
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }

    private void setData(boolean isRefresh, List data) {
        pageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapterFriends.setNewData(data);
        } else {
             if (size > 0) {
                mAdapterFriends.addData(data);
            }
        }
        if (size < pageSize) {
            mAdapterFriends.loadMoreEnd(isRefresh);
        } else {
            mAdapterFriends.loadMoreComplete();
        }
    }

    private void loadMore() {
        mPresenter.searchFriendMore(mContent, pageIndex, pageSize);
    }

    /**********************************************************************************************************/
    @Override
    public void showPeopleListSuccess(List<SearchFriendBo.UserFriendBean> workBoPageData) {
        mAdapterPeople.setNewData(workBoPageData);
    }

    @Override
    public void showListEmpty() {

        mAdapterPeople.setEmptyView(mEmptyView2.getEmptyView("暂无人脉"));
        mAdapterPeople.setNewData(null);
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageIndex = 1;
        mPresenter.searchFriend(mContent, pageIndex, pageSize);
    }


}
